<script>
import SelectTree from '@/views/employee/components/select-tree'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
import ImageUpload from '@/views/employee/components/image-upload'

export default {
  components: {
    SelectTree,
    ImageUpload
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: null,
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: ' 请选择聘用形式 ' }
        ],
        timeOfEntry: [
          { required: true, message: ' 请选择入职时间 ', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: ' 请选择转正时间 ', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!this.formData.timeOfEntry) {
                callback()
                return
              }
              if (new Date(this.formData.timeOfEntry) > new Date(value)) {
                callback(new Error('转正时间不能小于入职时间'))
              } else {
                callback()
              }
            }
          }
        ]
      }
    }
  },
  created() {
    // 回显员工信息
    if (this.$route.params.id) {
      this.getEmployeeDetail()
    }
  },
  methods: {
    // 回显员工信息
    async getEmployeeDetail() {
      this.formData = await getEmployeeDetail(this.$route.params.id)
    },
    saveData() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            await updateEmployee(this.formData)
            this.$message.success('修改员工成功')
          } else {
            await addEmployee(this.formData)
            this.$message.success('新增员工成功')
          }
          this.$router.push('/employee')
        }
      })
    }
  }
}
</script>

<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="formData" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formData.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="formData.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <!--:disabled的两个!可以取反再取反，相当于将非布尔值变成布尔值-->
                <el-input
                  v-model="formData.mobile"
                  :disabled="!!formData.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <SelectTree v-model="formData.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formData.formOfEmployment" size="mini" class="inputW">
                  <!--value加:可以将字符串变成数字-->
                  <el-option :value="1" label="正式" />
                  <el-option :value="2" label="非正式" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formData.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formData.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload :staff-photo.sync="formData.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px
  }
}

</style>
